// 引入用户模型
const { User } = require('../../model/User')
// 引入bcrypt
const bcrypt = require('bcrypt')
module.exports = async (req, res) => {
    const { email, password } = req.body
    if (req.body.email.length <= 0 || req.body.password.length <= 0) return res.send("邮件地址或密码错误")
    let user = await User.findOne({ email })
    if (user) {
        let isValid = await bcrypt.compare(password, user.password)
        if (isValid) {
            req.session.username = user.username
            // 一次登录处处使用
            req.app.locals.userInfo = user
            // 重新定向到用户列表页
            res.redirect('/admin/user')
        } else {
            res.send("邮箱或密码错误")
        }
    } else {
        res.send("邮箱或密码错误")
    }
}